Page({
  data: {
    active: 0,
    paixufangfa: "selectAll",
    shengjiang:true,
    isrepeat:false,
    course: []
  },
  shengjiangkongzhi:function(e){
      this.setData({
        shengjiang:!this.data.shengjiang
      })
      console.log("数据更新成功",this.data.shengjiang);
     
  },
  onChange: function (e) {
    const index = e.detail;
    console.log(index.index);
    let paixufangfa;
    switch (index.index) {
      case 0:
        paixufangfa = "selectAll";
        this.data.isrepeat=false
        break;
      case 1:
        paixufangfa = "selectAllOrderByStudyCount";
        this.data.isrepeat=false
        break;
      case 2:
        if(this.data.isrepeat==true)shengjiang=!this.data.shengjiang;
        if(this.data.shengjiang==true)paixufangfa="selectAllOrderByCurrentPriceDesc"
        else paixufangfa = "selectAllOrderByCurrentPrice";
        this.data.isrepeat=true;
        console.log(this.data.isrepeat,this.data.shengjiang)
        break;
      case 3:
        this.data.paixufangfa = "selectAllOrderByJiagequjian";
        break;
      default:
        this.data.paixufangfa = "selectAll";
    }
    this.setData({
      paixufangfa: paixufangfa
    });
    console.log(this.data.paixufangfa);
    this.fetchCoursesBySortMethod(this.data.paixufangfa);
  },

  fetchCoursesBySortMethod: function (sortMethod) {
    wx.request({
      url: `http://localhost:8080/ls/allcourse/${sortMethod}`,
      method: 'GET',
      success: (res) => {
        this.setData({
          course: res.data.data
        });
        console.log('数据更新成功:', this.data.course);
      },
      fail: (err) => {
        console.error('请求失败:', err);
      },
    });
  },

  handleCourseClick: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/course/ygmzh/ygmzh?id=${id}`,
      success: () => {
        console.log(`成功跳转到ygmzh页面`);
      },
      fail: (err) => {
        console.error('跳转失败:', err);
      }
    });
  },

  handleBack() {
    wx.navigateBack({
      delta: 1
    });
  },

  onLoad(options) {
    this.fetchCoursesBySortMethod(this.data.paixufangfa);
  },

  onReady() { },

  onShow() { },

  onHide() { },

  onUnload() { },

  onPullDownRefresh() { },

  onReachBottom() { },

  onShareAppMessage() { }
});